distutils_args.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from distutils.errors import DistutilsArgError
  2. from distutils.fancy_getopt import FancyGetopt
  3. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  4. if MYPY_CHECK_RUNNING:
  5. from typing import Dict, List
  6. _options = [
  7. ("exec-prefix=", None, ""),
  8. ("home=", None, ""),
  9. ("install-base=", None, ""),
  10. ("install-data=", None, ""),
  11. ("install-headers=", None, ""),
  12. ("install-lib=", None, ""),
  13. ("install-platlib=", None, ""),
  14. ("install-purelib=", None, ""),
  15. ("install-scripts=", None, ""),
  16. ("prefix=", None, ""),
  17. ("root=", None, ""),
  18. ("user", None, ""),
  19. ]
  20. # typeshed doesn't permit Tuple[str, None, str], see python/typeshed#3469.
  21. _distutils_getopt = FancyGetopt(_options) # type: ignore
  22. def parse_distutils_args(args):
  23. # type: (List[str]) -> Dict[str, str]
  24. """Parse provided arguments, returning an object that has the
  25. matched arguments.
  26. Any unknown arguments are ignored.
  27. """
  28. result = {}
  29. for arg in args:
  30. try:
  31. _, match = _distutils_getopt.getopt(args=[arg])
  32. except DistutilsArgError:
  33. # We don't care about any other options, which here may be
  34. # considered unrecognized since our option list is not
  35. # exhaustive.
  36. pass
  37. else:
  38. result.update(match.__dict__)
  39. return result